<?php

    class CicpAgilezenProjects implements Cicp_Handler_Interface
{
    function initGraph($x, $y, $z)
    {
        $graph = new Cicp_Graph();
        $graph->setPosition($x, $y, $z);

        $this->arrangeProjects($graph);

        return $graph;
    }

    function menuChoice(Cicp_Graph $graph, Cicp_Graph_Node $node, Cicp_Graph_Menu $menu, $menuButtonLabel)
    {
        // we have no menus in this graph, so there's nothing to do
        // here
        // to have an effect we simply read the projects another time
        // and arrange the sphere differently
        $this->clear($graph);
        $this->arrangeProjects($graph);

        return $graph;
    }

    function clear($graph)
    {
        $nodes = $graph->getNodes();
        foreach ($nodes as $node) {
            $graph->removeNode($node);
        }
    }

    function arrangeProjects(Cicp_Graph $graph)
    {
        // read xml file from agilezen
        $xmlObj = simplexml_load_file("http://agilezen.com/api/v1/projects?"
                                      . "apikey=6425ecaefcc3417fb8e5463690f7e3ac");

        $projectNode = new Cicp_Graph_Node;
        $projectNode->setColor(100, 100, 255);
        $projectNode->setHlColor(150, 150, 255);
        $projectNode->setShape('sph');
        $projectNode->setDimensions(0., .3, 0.);

        foreach ($xmlObj->items->project as $p) {
            $node = clone $projectNode;
            $node->setTitle($p->description);
            $node->setPosition(rand(0, 4), rand(0, 4), rand(0, 4));
            $graph->registerNode($node);
        }
    }
}